package com.borrowed.book.book.controller;

import com.borrowed.book.book.domain.DeleteBorrowsInDto;
import com.borrowed.book.book.domain.SBorrowOrder;
import com.borrowed.book.book.domain.SelectBorrowOrderInDto;
import com.borrowed.book.book.domain.SelectBorrowOrderOutDto;
import com.borrowed.book.book.service.BorrowOrderService;
import com.borrowed.book.system.controller.BaseController;
import com.borrowed.book.system.domain.AjaxResult;
import com.borrowed.book.system.domain.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "借阅订单管理")
@RestController
@RequestMapping("/api/borrow")
public class BorrowOrderController extends BaseController {
    @Autowired
    private BorrowOrderService borrowOrderService;

    @ApiOperation(value = "添加到待借书单",httpMethod = "POST")
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addBorrowOrder(Long bookId){
        AjaxResult result;
        try{
            borrowOrderService.AddBorrowOrderService(bookId);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "借阅订单一览查询",httpMethod = "GET")
    @GetMapping("/select")
    @ResponseBody
    @Transactional
    public TableDataInfo selectBorrowOrder(SelectBorrowOrderInDto inDto){
        startPage();
        List<SelectBorrowOrderOutDto> list=borrowOrderService.selectBorrowOrderList(inDto);
        return getDataTable(list);
    }



    @ApiOperation(value = "待借移至收藏书单",httpMethod = "PUT")
    @PutMapping("/collect")
    @ApiImplicitParam(name="id",value = "借阅订单id",type = "Long",required = true)
    @ResponseBody
    @Transactional
    public AjaxResult collectBorrowOrder(Long id){
        AjaxResult result;
        try{
            borrowOrderService.collectBorrowOrder(id);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除书单",httpMethod = "DELETE")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name="bookId",value = "bookId",type = "Long",required = true)
    @ResponseBody
    public AjaxResult deleteBorrowOrder(Long bookId){
        AjaxResult result;
        try{
            borrowOrderService.deleteBorrowOrderById(bookId);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "批量删除书单",httpMethod = "DELETE")
    @DeleteMapping("/deleteByIds")
    @ResponseBody
    public AjaxResult deleteBorrowOrders(@RequestBody DeleteBorrowsInDto borrowsInDto){
        AjaxResult result;
        try{
            borrowOrderService.deleteBorrowOrderByIds(borrowsInDto.getBookIds());
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }



}
